home *** CD-ROM | disk | FTP | other *** search
/ JCSM Shareware Collection 1996 September / JCSM Shareware Collection (JCS Distribution) (September 1996).ISO / gamgraph / sier10.zip / SIER_1.C < prev    next >
C/C++ Source or Header  |  1994-08-03  |  3KB  |  146 lines

  1. /****************** Sierpinsky Gasket, alias The Chaos Game ****************/
  2. /*  Alternate Form, with an extra "attractor inside the triangle           */
  3. /*                                                                         */
  4. /*        M\Cooper, Route 1, Box 204, Grantsville, MD 21536                */
  5. /*                       thegrendel@aol.com                                */
  6. /*                                                                         */
  7. /***************************************************************************/
  8.  
  9. #include <conio.h>
  10. #include <graphics.h>
  11. #include <stdlib.h>
  12. #include <time.h>
  13.  
  14. #define NUMBER_OF_VERTICES 4
  15. #define POINT_COUNT (NUMBER_OF_VERTICES + 1)
  16. #define CIRCLE_RADIUS 4
  17. #define TRIANGLE_COLOR RED
  18. #define CIRCLE_COLOR CYAN
  19. #define BACKGROUND_COLOR WHITE
  20. #define MAX_SCREEN_WIDTH 639
  21. #define MAX_SCREEN_HEIGHT 479
  22. #define MID_DIVISOR 2
  23. #define V0x 320
  24. #define V0y 10
  25. #define V1x 10
  26. #define V1y 450
  27. #define V2x 630
  28. #define V2y 450
  29. #define W0x 320
  30. #define W0y 230
  31.  
  32.  
  33.    typedef struct { int x_coord, y_coord; } Point;
  34.    int triangle[ POINT_COUNT * 2 ] =
  35.                  { V0x, V0y, V1x, V1y, V2x, V2y, V0x, V0y };
  36.    Point V[NUMBER_OF_VERTICES] = {
  37.                  { V0x, V0y },
  38.                  { V1x, V1y },
  39.                  { V2x, V2y },
  40.                  { W0x, W0y }
  41.                  };
  42.  
  43.    void graphics_setup( int background_color );
  44.    void draw_triangle();
  45.    void set_up();
  46.    void Sierpinsky();
  47.    Point get_seed();
  48.    Point calculate_midpoint( Point p1, Point p2 );
  49.  
  50.  
  51. void main()
  52. {
  53.   
  54.       set_up();
  55.      
  56.       Sierpinsky();
  57.  
  58.       getch(); // Stop the action...
  59.       getch(); // Wait for new keypress.
  60.       closegraph();
  61. }        
  62.  
  63. void set_up()
  64. {
  65.  
  66.       randomize();
  67.       graphics_setup( BACKGROUND_COLOR );
  68.       setlinestyle( SOLID_LINE, 1, NORM_WIDTH );
  69.       draw_triangle();
  70.  
  71.       return;
  72.  
  73. }
  74.  
  75. void graphics_setup( int background_color )
  76. {
  77.    int grdriver = VGA,
  78.        grmode = VGAHI;
  79.  
  80.        registerfarbgidriver( EGAVGA_driver_far );
  81.        registerfarbgifont( triplex_font_far );
  82.        initgraph( &grdriver, &grmode, "" );
  83.        setbkcolor( background_color );
  84.  
  85.        return;
  86.  
  87. }
  88.  
  89. void draw_triangle( )
  90. {
  91.  
  92.       setcolor( TRIANGLE_COLOR );
  93.       drawpoly( POINT_COUNT, triangle );
  94.  
  95.       return;
  96. }
  97.  
  98. Point get_seed()
  99. {
  100.    Point Initial;
  101.  
  102.    Initial.x_coord = random( MAX_SCREEN_WIDTH );
  103.    Initial.y_coord = random( MAX_SCREEN_HEIGHT ); 
  104.  
  105.       return ( Initial );
  106. }
  107.  
  108. Point calculate_midpoint( Point p1, Point p2 )
  109. {
  110.    Point mid;
  111.  
  112.       mid.x_coord = ( p2.x_coord + p1.x_coord ) / MID_DIVISOR;
  113.  
  114.       mid.y_coord = ( p2.y_coord + p1.y_coord ) / MID_DIVISOR;
  115.  
  116.       return( mid );
  117.  
  118.  
  119. void Sierpinsky()
  120. {
  121.    Point Pi,
  122.       P,
  123.       Pnext;
  124.     int pixel_color;
  125.     register int t;
  126.  
  127.       Pi = get_seed();
  128.       P = Pi;
  129.       putpixel( P.x_coord, P.y_coord, pixel_color );
  130.       setcolor( CIRCLE_COLOR );
  131.       circle( P.x_coord, P.y_coord, CIRCLE_RADIUS );
  132.  
  133.       while( !kbhit() )
  134.      {
  135.      t = random( NUMBER_OF_VERTICES );
  136.  
  137.      Pnext = calculate_midpoint( P, V[t] );
  138.      putpixel( Pnext.x_coord, Pnext.y_coord, 2 * t + 1 );
  139.      P = Pnext;
  140.      }
  141.  
  142.       return;
  143.  
  144. }
  145.